package edu.szu.huhanpeng.doodlingFight.view.components 
{
	import edu.szu.huhanpeng.doodlingFight.AppFacade;
	import edu.szu.huhanpeng.doodlingFight.globalConst.GlobalConst;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessData;
	import edu.szu.huhanpeng.doodlingFight.material.ChessView;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 单个棋子
	 * @author Huhanpeng
	 */
	public class ChessComponent extends Sprite
	{
		private static const DEFAULT:int = 1;	//默认状态
		private static const OVER:int = 2;		//鼠标移上
		private static const SELECTED:int = 3;	//选中状态
		private static const FADE_OUT:int = 4;  //消除中
		
		private static const DEFAULT_TYPE:int = 1;	//默认类型
		private var _type:int; //类型号，对应_typeView的帧数
		
		private var _view:ChessView;  //共有4帧，对应 1普通，2rollover, 3选中，4消除中
		private var _typeView:MovieClip;
		
		private var _selected:Boolean = false;
		
		//private var _id:int = 0;	//标识，name的一部分，每生成一次就++
		
		/////////////////////////////////////////////////////////////////////
		//构造函数                                                         //
		/////////////////////////////////////////////////////////////////////
		/**
		 * 
		 * @param	type 棋子的类型
		 */
		public function ChessComponent( type:int = DEFAULT_TYPE ) 
		{
			init( type );
		}
		
		private function init( type:int ):void 
		{
			_view = new ChessView();
			_typeView = _view.type;
			
			_view.gotoAndStop( DEFAULT );
			_typeView.gotoAndStop( type );
			
			_view.scaleX = _view.scaleY = GlobalConst.setting.CHESS_SCALE;
			
			addChild( _view );
		}
		
		/////////////////////////////////////////////////////////////////////
		//getter setter                                                    //
		/////////////////////////////////////////////////////////////////////
		public function set type( value:int ):void
		{
			try 
			{
				if ( value < 1 || value > _typeView.totalFrames ) 
				{
					throw new Error( "棋子类型设置错误" );
				}
			} catch ( e:Error ) {
				//捕获错误，如果是debug版则重新抛出，否则把值置为1；
				if (GlobalConst.setting.DEBUG == true ) 
				{
					throw new Error( "棋子类型设置错误" );
				}
				else
				{
					(AppFacade.getInstance().retrieveProxy(name).getData() as IChessData).type = 1;
					value = 1;
				}
			} finally {
				_type = value;
				_typeView.gotoAndStop( value );
			}
		}
		
		public function get type():int
		{
			return _type;
		}
		
		public function get view():DisplayObject
		{
			return _view;
		}
		
		public function get selected():Boolean 
		{
			return _selected;
		}
		
		public function set selected(value:Boolean):void 
		{
			if ( _selected == value ) 
			{
				return;
			}
			
			_selected = value;
			updateState();
		}
		

		
		/////////////////////////////////////////////////////////////////////
		//接口                                                             //
		/////////////////////////////////////////////////////////////////////
		public function addEvent():void 
		{
			_view.addEventListener( MouseEvent.ROLL_OVER, onRollOver );
			_view.addEventListener( MouseEvent.ROLL_OUT, onRollOut );
			_view.addEventListener( MouseEvent.CLICK, onClick );
		}
		

		
		public function removeEvent():void
		{
			_view.removeEventListener( MouseEvent.ROLL_OVER, onRollOver );
			_view.removeEventListener( MouseEvent.CLICK, onClick );
		}
		
		public function disableClick():void
		{
			_view.removeEventListener( MouseEvent.CLICK, onClick );
		}
		
		public function enableClick():void
		{
			_view.addEventListener( MouseEvent.CLICK, onClick );
		}
		
		/////////////////////////////////////////////////////////////////////
		//内部                                                             //
		/////////////////////////////////////////////////////////////////////
		private function onClick(e:MouseEvent):void 
		{
			_selected = !_selected;
			updateState();
			dispatchEvent(new Event( GlobalConst.event.CHESS_CLICK ) );
		}
		
		private function onRollOver(e:MouseEvent):void 
		{
			if ( !_selected ) 
			{
				_view.gotoAndStop( OVER );
			}
			
		}
		
		private function onRollOut(e:MouseEvent):void 
		{
			if ( !_selected ) 
			{
				_view.gotoAndStop( DEFAULT );
			}
		}
		
		private function updateState():void 
		{
			if ( _selected ) 
			{
				_view.gotoAndStop( SELECTED );
			}
			else
			{
				_view.gotoAndStop( DEFAULT );
			}
		}
		
	}

}